package com.hsck.order.kds.main.fragment.record

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hsck.order.kds.base.utils.setOnSingleClickListener
import com.hsck.order.kds.databinding.FoodDeliveryRecordItemLayoutBinding
import com.hsck.order.kds.main.fragment.standard.bean.DishManager
import com.hsck.order.kds.main.fragment.standard.bean.TestListBean

class FoodDeliveryRecordAdapter(val activity: Activity, val columns: Int) :
    RecyclerView.Adapter<FoodDeliveryRecordAdapter.ItemVH>() {

    private val diffUtil = object : DiffUtil.ItemCallback<TestListBean>() {
        override fun areItemsTheSame(oldItem: TestListBean, newItem: TestListBean): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: TestListBean, newItem: TestListBean): Boolean {
            return oldItem == newItem
        }
    }
    private val asyncListDiffer = AsyncListDiffer(this, diffUtil)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVH {
        return ItemVH(
            FoodDeliveryRecordItemLayoutBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    inner class ItemVH(val binding: FoodDeliveryRecordItemLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindData(position: Int) {
            val item = asyncListDiffer.currentList[position]
            if (position == asyncListDiffer.currentList.size - 1) {
                binding.ivDivider.visibility = View.INVISIBLE
            } else {
                binding.ivDivider.visibility = View.VISIBLE
            }
            binding.tvRestoreDish.setOnSingleClickListener {
                onRestoreDishListener.onRestoreDishClick(item, position)
            }
            initRecyclerView(binding)
        }

        private fun initRecyclerView(binding: FoodDeliveryRecordItemLayoutBinding) {
            val foodDeliveryRecordAdapter = FoodDeliveryRecordHrItemsAdapter(activity, columns)
            val layoutManager = LinearLayoutManager(activity)
            layoutManager.isItemPrefetchEnabled = true
            layoutManager.orientation = LinearLayoutManager.HORIZONTAL
            binding.recyclerView.layoutManager = layoutManager
            binding.recyclerView.adapter = foodDeliveryRecordAdapter
            foodDeliveryRecordAdapter.submitList(DishManager().getDishList())
        }
    }

    fun submitList(dataResponse: List<TestListBean>) {
        asyncListDiffer.submitList(dataResponse)
    }

    fun addItemAfterId(newItem: TestListBean, targetId: String) {
        val newList = asyncListDiffer.currentList.toMutableList()
        val targetIndex = newList.indexOfFirst { it.id == targetId }
        if (targetIndex != -1) {
            newList.add(targetIndex, newItem)
            asyncListDiffer.submitList(newList)
        }
    }

    fun removeItemAfterId(targetId: String) {
        val newList = asyncListDiffer.currentList.toMutableList()
        val targetIndex = newList.indexOfFirst { it.id == targetId }
        if (targetIndex != -1) {
            newList.removeAt(targetIndex)
            asyncListDiffer.submitList(newList)
        }
    }

    fun removeItemAfterIndex(targetIndex: Int) {
        if (targetIndex < asyncListDiffer.currentList.size) {
            val newList = asyncListDiffer.currentList.toMutableList()
            newList.removeAt(targetIndex)
            asyncListDiffer.submitList(newList)
        }
    }

    override fun onBindViewHolder(holder: ItemVH, position: Int) {
        holder.bindData(position)
    }

    override fun getItemCount(): Int {
        return asyncListDiffer.currentList.size
    }


    private lateinit var onRestoreDishListener: RestoreDishListener
    fun setRestoreDishListener(onRestoreDishListener: RestoreDishListener) {
        this.onRestoreDishListener = onRestoreDishListener
    }

    interface RestoreDishListener {
        fun onRestoreDishClick(itemBean: TestListBean, position: Int)
    }
}